package com.sh.线程安全相关.synchronized代码块;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class MyThread extends Thread {
    static int tickets = 0;

    static Object lock = new Object();

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            while (true) {
                synchronized (lock) {
                    if (tickets < 100) {
                        tickets++;
                        System.out.println(Thread.currentThread().getName() + "正在卖" + tickets);
                        try {
                            Thread.sleep(100L);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        break;
                    }
                }
            }
        }
    }
}

